<template>
  <el-card shadow="never">
    <!-- 搜索 -->
    <el-form :model="searchForm" class="mb-3" size="small">
      <el-row>
        <el-col :span="6">
          <el-form-item label="关键词">
            <el-input
              v-model="searchForm.keyword"
              placeholder="管理员昵称"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="6">
          <el-button type="primary" @click="getData">搜索</el-button>
          <el-button @click="resetSearchForm">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 新增刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>

    <!-- 表格 -->

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="会员ID" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="integrate" label="積分" />
      <el-table-column prop="email" label="郵箱" />
      <el-table-column prop="mobile" label="電話號碼" />
      <el-table-column prop="wallet" label="餘額" />
      <el-table-column prop="status" label="狀態">
        <template #default="{ row }">
          {{ row.status == 1 ? "正常" : "禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="是否禁用">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange($event, row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" text type="primary" @click="handleEdit(row)"
            >修改</el-button
          >

          <el-popconfirm
            title="是否要删除?"
            confirm-button-text="确定"
            cancel-button-text="取消"
            @confirm="handleDelete(row.id)"
          >
            <template #reference>
              <el-button class="!m-0" text type="primary" size="small"
                >删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      class="flex justify-center mt-4"
      @current-change="getData"
      :current-page="curpage"
      :page-size="limit"
    />

    <!-- 提交表单 -->
    <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
        <el-form-item label="用戶名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密碼" prop="password">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="積分" prop="integrate">
          <el-input v-model="form.integrate"></el-input>
        </el-form-item>

        <!-- <el-form-item label="角色">
          <el-select v-model="form.role_id" placeholder="选择角色">
            <el-option
              v-for="item in role"
              :key="item.id"
              :label="item.role_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
    </FormDrawer>
  </el-card>
</template>

<script setup>
import { ref } from "vue";

import {
  getMember,
  createMember,
  updateMember,
  deleteMember,
} from "~/api/member.js";
import FormDrawer from "~/components/FormDrawer.vue";
import ListHeader from "~/components/ListHeader.vue";

import { useInitTable, useInitForm } from "~/composables/useCommon.js";

const role = ref(null);

const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  curpage,
  total,
  limit,
  getData,
  handleDelete,
  handleStatusChange,
} = useInitTable({
  searchForm: {
    keyword: "",
  },
  updateStatus: updateMember,
  getList: getMember,
  delete: deleteMember,
  //   onGetListSuccess: (res) => {

  //     (total.value = res.data.total), (tableData.value = res.data.data);
  //   },
});

//表单

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
} = useInitForm({
  form: {
    username: "",
    password: "",
    integrate: "",
  },
  rules: {
    username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
  },
  getData,
  update: updateMember,
  create: createMember,
});
</script>
